package com.aau.edu.config;

import com.aau.edu.service.ServiceA;
import com.aau.edu.service.ServiceB;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Classname ConfigBean
 * @Date 2024/12/18 17:11
 * @Created by 鲍宪立
 */

/***
 * 考虑不加@Configuration会怎么样？
 * 加不加的主要区别在于被@Configuration修饰的类,spring容器中会通过cglib给这个类创建一个代理，代理会拦截所有被@Bean修饰的方法，
 * 默认情况（bean为单例）下确保这些方法只会被调用一次，从而确保这些bean是同一个bean，即单例的。
 */
@Configuration
public class ConfigBean2 {

    @Bean
    public ServiceA serviceA() {
        System.out.println("调用serviceA()方法"); //@0
        return new ServiceA();
    }

    @Bean
    ServiceB serviceB1() {
        System.out.println("调用serviceB1()方法");
        ServiceA serviceA = this.serviceA(); //@1
        return new ServiceB(serviceA);
    }

    @Bean
    ServiceB serviceB2() {
        System.out.println("调用serviceB2()方法");
        ServiceA serviceA = this.serviceA(); //@2
        return new ServiceB(serviceA);
    }

}
